**********************************************************************************
*This file builds vars on migration flows in long-differences from the Census/ACS*
**********************************************************************************

use "$list/czones_list.dta", clear

merge 1:1 czone using "$clean_data_lmarket/czone1990_MigrationFlows.dta", assert(3) nogenerate
merge 1:1 czone using "$clean_data_lmarket/czone2000_MigrationFlows.dta", assert(3) nogenerate
merge 1:1 czone using "$clean_data_lmarket/czone2008_MigrationFlows.dta", assert(3) nogenerate

*Normalized (to 1 year) from Census
foreach year in 1990 2000 {
foreach age in a19_64 a19_34 {

*Share of students who are migrants
gen share_MigStudN4_`age'_`year' = 100*(ipums_migrant_sc_`age'_`year'/4)/ipums_pop_sc_`age'_`year'
gen share_MigStudN5_`age'_`year' = 100*(ipums_migrant_sc_`age'_`year'/5)/ipums_pop_sc_`age'_`year'

*Share of migrants
gen share_MigN4_`age'_`year' = 100*(ipums_migrant_`age'_`year'/4)/ipums_pop_`age'_`year'
gen share_MigN5_`age'_`year' = 100*(ipums_migrant_`age'_`year'/5)/ipums_pop_`age'_`year'
}
}

*Already 1-year migrants from ACS
foreach year in 2008 {
foreach age in a19_64 a19_34 {

*Share of students who are migrants
gen share_MigStud_`age'_`year' = 100*(ipums_migrant_sc_`age'_`year')/ipums_pop_sc_`age'_`year'

*Share of migrants
gen share_Mig_`age'_`year' = 100*(ipums_migrant_`age'_`year')/ipums_pop_`age'_`year'

}
}

*Changes
foreach nr in N4 N5 {
foreach age in a19_64 a19_34 {
foreach var in MigStud Mig {

gen d_`var'`nr'_`age' = (share_`var'_`age'_2008-share_`var'`nr'_`age'_1990)

}
}
}

rename ipums_pop_a19_64_1990 ipums_popMig_1990

*Keep variables of interest
keep czone share*_1990 d_* ipums_popMig_1990


save "$final_data_outcomes/czone_StudentMigrantsLongDifferences.dta", replace

